<?php
class Wechat_Model extends CI_Model{
	
	public $common_model;
	public $openId;

	function __construct(){
		parent::__construct();
// 		$this->load->model('common_model');
// 		$this->common_model = &get_instance()->common_model;
		$this->config->load('_myconfig.php');
		
	}
	
	
	/**
	 * 通过code获取openId
	 */
	function getOpenId($code){
		// post get 不论
// 		$code = isset($_POST['code'])?$_POST['code']:(isset($_GET['code'])?$_GET['code']:'');
		$appid = $this->config->item('appid');
		$secret = $this->config->item('secret');
		$url = $this->config->item('wechat_url_1');
		
		$url = str_replace('APPID',$appid,$url);
		$url = str_replace('SECRET',$secret,$url);
		$url = str_replace('CODE',$code,$url);
		
		$weixin =  file_get_contents($url);//通过code换取网页授权access_token
		$jsondecode = json_decode($weixin); //对JSON格式的字符串进行编码
		$array = get_object_vars($jsondecode);//转换成数组
		return isset($array['openid'])?($array['openid']):'';//输出openid
		
	}
	
	
	
	
	
	/**
	 * to judge registe whether or not 
	 * @param unknown $id
	 * @return unknown
	 */
	function get_user(){
		$openId = isset($_POST['openId'])?$_POST['openId']:(isset($_GET['openId'])?$_GET['openId']:'');
		
		return $openId;
		
	}
	
	
	




}